<?php
/**
 * @author hjj
 * 国免优述 供应商订单
 * 供应商申报支付单、订单和清单
 */
namespace App\Service\OrderService\OrderRouteService;

use App\Service\Code\Normal;
use App\Model\PssModel\OrderSupplier;
use Service\AppService\IdService;
use App\Service\OrderService\BaseSupplierRouterService;
use App\Service\VendorClient\ShopgmysApi\ShopGmysClient;
use App\Model\CommodityModel\SupplierCommodity;

class Shopgmys extends BaseSupplierRouterService {
    /**
     * 创建订单
     * @param array $param
     * @return mixed
     */
    public function createOrder($param = []) {
        $orderItems = [];
        $supplierGoodsInfo  = $param['supplierGoodsInfo'];
        $userGoodsList      = $supplierGoodsInfo['userGoodsList'];
        $goodsList          = $supplierGoodsInfo['goodsList'];
        $supplierCode       = $supplierGoodsInfo['supplierCode'];
        $currentGoods       = current($goodsList);
        $supplierCommodityType = $currentGoods['supplierCommodityType'];

        //下游是否申报
        $isDelare = (
            (isset($param['order']['isOrderDeclare']) && $param['order']['isOrderDeclare'] == 1) ||
            (isset($param['order']['isPayDeclare']) && $param['order']['isPayDeclare'] == 1)
            ) ? true : false;

        //是否云仓商城
        $isShop = isset($param['order']['isSplitOrder']) && $param['order']['isSplitOrder'] == 1 ? true : false ;

        $param['order']['supplier_psn'] = OrderSupplier::instance()->getCreateOrderSn($supplierCode);
        $param['order']['supplier_csn'] = IdService::instance()->getOtherID();

        if( $isDelare && !$isShop && $supplierCommodityType != GOODS_TYPE_YBMY ) {
            $param['order']['supplier_dsn'] = (string)$param['order']['orderSn'];
        } else {
            $param['order']['supplier_dsn'] = (string)IdService::instance()->getOtherID();
        }
        // $param['order']['isQueryExpress'] = 1;
        $res = OrderSupplier::instance()->getSupplierCreateOrderData($param);
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }

        $orderItems[] = $res['data'];
        return Normal::returnCode(Normal::SUC,"获取成功",[
            'supplierCode' => $supplierCode,
            'orderItems' => $orderItems
        ]);
    }

    /**
     * 订单支付
     * @param array $param
     * @return mixed
     */
    public function orderPay($param = []) {
        $goodsList   = $param['orderGoods'];
        // echo json_encode($param, 320);die;
        $supplierSkuIds = array_column($goodsList, 'supplier_skuid');
        $goodsIdArr     = SupplierCommodity::whereIn('ID', $supplierSkuIds)->pluck('SupplierCommodityKey', 'ID')->toArray();
        foreach ($param['orderGoods'] as &$item) {
            if(isset($goodsIdArr[$item['supplier_skuid']])){
                $item['supplier_commodity_key'] = $goodsIdArr[$item['supplier_skuid']];
            }
            unset($item);
        }
        return $this->callSdkApi(ShopGmysClient::class, $param['apiBody'])->createOrder($param);
    }

    /**
     * 订单取消
     * @param array $param
     * @return mixed
     */
    public function orderCancel($param = []) {
        return Normal::returnCode(Normal::SUC, "处理完成");
    }

    /**
     * 订单售后
     * @param array $param
     * @return mixed
     */
    public function orderAfterSale($param = []) {
        return Normal::returnCode(Normal::SUC, "处理完成");
    }


    /**
     * 商品列表查询接口
     * @param array $param
     * @return mixed
     */
    public function queryGoodsList($param = []) {
        $res = $this->callSdkApi(ShopGmysClient::class, $param['apiBody'])->{__FUNCTION__}($param['queryBody']);
        if ($res['code'] != Normal::SUC) {
            return $res;
        }
        return Normal::returnCode(Normal::SUC, "获取成功", [
            'total' => $res['data']['total'],
            'list'  => $res['data']['list']
        ]);
    }

    /**
     * 查询物流
     * @param array $param
     * @return mixed
     */
    public function queryOrderExpress($param) {
        //物流对方通知
        return Normal::returnCode(Normal::SUC, "处理完成");
    }
}